<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商" prop="dsid">
        <el-select v-model="queryParams.dsid" placeholder="请选择供应商" clearable size="small">
          <el-option
            v-for="supplier in supplierList"
            :key="supplier.dsid"
            :label="supplier.dsName"
            :value="supplier.dsid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="applyUser">
        <el-input
          v-model="queryParams.applyUser"
          placeholder="请输入申请人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="dpState">
        <el-select v-model="queryParams.dpState" placeholder="单据状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['drug:purchase:addPurchase']"
        >新增采购</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="submitAudit"
          v-hasPermi="['drug:purchase:update']"
        >提交审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="inStorage"
          v-hasPermi="['drug:purchase:update']"
        >提交入库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['drug:purchase:delete']"
        >作废</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange" border="0px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据ID" align="center"  :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/drug/purchase/data/' + scope.row.dpid+'/'+scope.row.dpState" class="link-type">
            <span>{{ scope.row.dpid }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="druSupplier.dsName"/>
      <el-table-column label="采购批发总额" align="center" prop="dpTotalAmount" />
      <el-table-column label="状态" align="center" prop="dpState" :formatter="statusFormat" />
      <el-table-column label="申请人" align="center" prop="applyUser" />
      <el-table-column label="入库操作人" align="center" prop="operationUser" />
      <el-table-column label="入库时间" align="center" prop="enterDate" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产厂家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="厂家名称" prop="dmName">
          <el-input v-model="form.dmName" placeholder="请输入厂家名称" />
        </el-form-item>
        <el-form-item label="厂家编码" prop="dmCoding">
          <el-input v-model="form.dmCoding" placeholder="请输入厂家编码" />
        </el-form-item>
        <el-form-item label="联系人" prop="dmContactName">
          <el-input v-model="form.dmContactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="dmPhone">
          <el-input v-model="form.dmPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="关键字" prop="dmKeyword">
          <el-input v-model="form.dmKeyword" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item label="地址" prop="dmSite">
          <el-input v-model="form.dmSite" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="状态" prop="dmState">
          <el-radio-group v-model="form.dmState">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {selectSupplier,selectAll,submitAudit,deletePurchase} from "@/api/drug/purchase";

  export default {
    name: "purchase",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数据
        ids: [],
        purchases: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 厂家表格数据
        purchaseList: [],
        // 供应商全部数据
        supplierList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dsid: undefined,
          applyUser: undefined,
          dpState: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          dmName: [
            { required: true, message: "厂家名称不能为空", trigger: "blur" }
          ],
          dmCoding: [
            { required: true, message: "厂家编码不能为空", trigger: "blur" }
          ],
          dmPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              required: true,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          dmContactName: [
            { required: true, message: "联系人不能为空", trigger: "blur" }
          ],
          dmSite: [
            { required: true, message: "地址不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getSupplier();
      this.getDicts("dru_purchase_state\n").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询生产厂家列表 */
      getList() {
        this.loading = true;
        selectAll(this.queryParams).then(response => {
          this.purchaseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询供应商列表 */
      getSupplier() {
        this.loading = true;
        selectSupplier().then(response => {
          this.supplierList = response.data ;
        });
      },
      // 生产厂家状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.dpState);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dmid: undefined,
          dmName: undefined,
          dmKeyword: undefined,
          dmPhone: undefined,
          dmCoding: undefined,
          dmContactName: undefined,
          dmState: "0",
          dmSite: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.purchases = selection.map(item => item)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push({name:'addPurchase'})
      },
      /** 提交审核操作 */
      submitAudit() {
        this.ids = [];
        var flag = true;
        for(var i = 0; i < this.purchases.length; i++){
          if (this.purchases[i].dpState != 0){
            this.ids.push(this.purchases[i].dpid)
          } else {
            this.msgError("入库成功的订单不可以提交审核");
            flag = false;
          }
        }
        if (flag){
          submitAudit(this.ids,"1").then(response => {
            this.msgSuccess("提交成功");
            this.getList();
          });
        }
      },
      /** 提交入库操作 */
      inStorage() {
        this.ids = [];
        var flag = true;
        for(var i = 0; i < this.purchases.length; i++){
          if (this.purchases[i].dpState == 3){
            this.ids.push(this.purchases[i].dpid)
          } else {
            this.msgError("只有审核通过的订单才可以入库");
            flag = false;
          }
        }
        if (flag){
          submitAudit(this.ids,"0").then(response => {
            this.msgSuccess("入库成功");
            this.getList();
          });
        }
      },
      /** 作废按钮操作 */
      handleDelete() {
        this.ids = [];
        for(var i = 0; i < this.purchases.length; i++){
          this.ids.push(this.purchases[i].dpid)
        }
        var dpids = this.ids;
        this.$confirm('是否确认删除选中的"' + this.ids.length + '"条数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deletePurchase(dpids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      }
    }
  };
</script>
